package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysMaterialParam;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.entity.po.SysMaterialType;
import com.xinsoft.service.SysMaterialService;
import com.xinsoft.service.SysMaterialTypeService;
import com.xinsoft.service.SysNoticeService;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SysMaterialController
 * @Description 物料管理控制层
 * @Author xuhb
 * @Date 2023-5-17 09:51:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/material")
@Api(value = "SysMaterialController", tags = {"物料管理控制层"})
public class SysMaterialController {
    @Autowired
    private SysMaterialTypeService sysMaterialTypeService;

    @Autowired
    private SysMaterialService sysMaterialService;

    @Autowired
    private SysNoticeService sysNoticeService;




    @ApiOperation("新增物料分类")
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysMaterialType materialType) {
        // 参数校验
        ValidatorUtils.validateEntity(materialType);
        String msg = sysMaterialTypeService.saveMaterialType(materialType);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    @ApiOperation("新增物料")
    @PostMapping("/addMaterial")
    public Response<?> addMaterial(@RequestBody SysMaterial material) {
        // 参数校验
        ValidatorUtils.validateEntity(material);
        material.setAuditId(Const.ADMIN_UID);
        material.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
        String msg = sysMaterialService.saveMaterial(material);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 更新物料分类
     * @Date 2023/5/17 9:57
     * @params materialType
     **/
    @ApiOperation("更新物料分类")
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody SysMaterialType materialType) {
        // 参数校验
        ValidatorUtils.validateEntity(materialType);
        String msg = sysMaterialTypeService.updateMaterialType(materialType);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    @ApiOperation("更新物料")
    @PostMapping("/editMaterial")
    public Response<?> editMaterial(@RequestBody SysMaterial material) {
        // 参数校验
        ValidatorUtils.validateEntity(material);
        String msg = sysMaterialService.editMaterial(material);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    @ApiOperation("绑定工艺路线")
    @GetMapping("/bindingCraft")
    public Response<?> editMaterial(@RequestParam("id") Integer id,@RequestParam("craftId") Integer craftId) {
        // 参数校验
        SysMaterial sysMaterial = sysMaterialService.getMaterialDetail(id);
        sysMaterial.setCraftId(craftId);
        String msg = sysMaterialService.editMaterial(sysMaterial);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    @ApiOperation("审核物料")
    @PostMapping("/auditMaterial")
    public Response<?> auditMaterial(@RequestBody List<Integer> ids) {
        Integer auditId =  TokenUtil.getUser().getId();
        List<SysMaterial> materialList = new ArrayList<SysMaterial>();
        Boolean errorFlag = true;
        // 查看之前是否已审核通过
        for(Integer id:ids){
            SysMaterial material = new SysMaterial();
            material = sysMaterialService.getById(id);
            if(material!=null){
                errorFlag = false;
                material.setAuditId(auditId);
                material.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
                materialList.add(material);
            }
        }
        if(errorFlag){
            return Response.fail("物料不存在,请确认");
        }else{
            sysMaterialService.updateBatchById(materialList);

            // 发送通知
            for (Integer id : ids) {
                List<String> paramValues = new ArrayList<>();
                paramValues.add(id.toString());
                Integer actionId  = Const.ACTION.MATERIAL_AUDIT;
                sysNoticeService.sendNoticeByActionId(paramValues,actionId);
            }

            return Response.succeed();
        }
    }

    @ApiOperation("反审核物料")
    @PostMapping("/noAuditMaterial")
    public Response<?> noAuditMaterial(@RequestBody List<Integer> ids) {
        // 查看之前是否已反审核
        Integer auditId =  TokenUtil.getUser().getId();
        List<SysMaterial> materialList = new ArrayList<SysMaterial>();
        Boolean errorFlag = true;
        // 查看之前是否已审核通过
        for(Integer id:ids){
            SysMaterial material = new SysMaterial();
            material = sysMaterialService.getById(id);
            if(material!=null){
                errorFlag = false;
                material.setAuditId(auditId);
                material.setAuditStatus(Const.AUDIT_STATUS_WAIT);
                materialList.add(material);
            }
        }
        if(errorFlag){
            return Response.fail("物料不存在,请确认");
        }else{
            sysMaterialService.updateBatchById(materialList);
            return Response.succeed();
        }
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 获取物料类别不分页信息
     * @Date 2023/5/19 9:57
     * @params param
     **/
    @ApiOperation("获取物料类别不分页信息")
    @GetMapping("/listMtype")
    public Response<?> listMtype(@RequestParam("mtypeName") String mtypeName) {
        return Response.succeed(sysMaterialTypeService.listMtype(mtypeName),"查询成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 删除物料分类
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除物料分类")
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        String msg = sysMaterialTypeService.deleteMaterialTypeById(ids);
        if(StringUtils.isEmpty(msg)){
            return Response.succeed();
        }else {
            return Response.fail(msg);
        }
    }

    @ApiOperation("删除物料明细")
    @PostMapping("/delMaterial")
    public Response<?> delMaterial(@RequestBody List<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            SysMaterial sysMaterial = sysMaterialService.getById(ids.get(i));
            if(sysMaterial.getAuditStatus().equals(Const.AUDIT_STATUS_NORMAL)){
                return Response.fail("存在已审核物料，不允许删除");
            }
        }
        sysMaterialService.deleteMaterial(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 获取物料明细分页
     * @Date 2023/5/19 9:57
     * @params param
     **/
    @ApiOperation("获取物料明细分页")
    @PostMapping("/listPageMaterial")
    public Response<?> listPageMaterial(@RequestBody SysMaterialParam param, HttpServletRequest request) {
        return Response.succeed(sysMaterialService.listPageMaterial(param,request));
    }

    @ApiOperation("获取物料詳細")
    @GetMapping("/getMaterialDetail")
    public Response<?> getMaterialDetail(@RequestParam("id") Integer id) {
        return Response.succeed(sysMaterialService.getMaterialDetail(id));
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) throws IOException {

        return Response.succeed(sysMaterialService.importExcel(multipartFile,response));
    }

}
